import { ref, watch, type Ref } from 'vue'
import { DoGet } from '@/utils/http'
import type { DishEntity } from '@/shared/entity/dish'

export const useDishRecord = (id?: Ref<number | undefined, number | undefined>) => {
  const result = ref<DishEntity>()

  const sendRequest = async (id?: number) => {
    if (id) {
      const resp = await DoGet<DishEntity>(`/dish/${id}`)
      result.value = resp.data
    }
  }

  watch(
    () => id?.value,
    val => {
      sendRequest(val)
    },
    {
      immediate: true
    }
  )

  return result
}
